<?php

namespace App\Model;

use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\ORM\Utility\Schema\Table;
use Throwable;

class ShopOrderItemModel extends AbstractModel
{
    protected $tableName = 't_shop_order_item';
    protected $autoTimeStamp = true; // 自动写入create_time和update_time 这两个字段是int

    /**
     * @throws Exception
     * @throws Throwable
     * @throws Exception|Throwable
     */
    public function getOrder($where)
    {
        $where['is_deleted'] = 0;
        return $this->order('id', 'desc')->get($where);
    }

    /**
     * @throws Throwable
     * @throws Exception
     */
    public function addOrder($where)
    {
        $this->data($where, false);
        return $this->save();
    }

    /**
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function addAll($par): array
    {
        return $this->saveAll($par);
    }

    public function schemaInfo(bool $isCache = true): Table
    {
        $table = new Table($this->tableName);

        $table->setIfNotExists();
        $table->setTableEngine(Engine::INNODB);
        $table->setTableCharset(Character::UTF8MB4_GENERAL_CI);
        $table->setTableComment('商城-订单-商品');

        $table->colInt('id')->setIsAutoIncrement()->setIsPrimaryKey(true)->setColumnComment("ID");
        $table->colBigInt('uuid', 20)->setDefaultValue(0)->setIsNotNull()->setColumnComment('下单用户编号');
        $table->colInt('sid', 25)->setDefaultValue(0)->setIsNotNull()->setColumnComment('商户id');
        $table->colVarChar('order_no', 20)->setDefaultValue('')->setColumnComment('商品订单单号');
        $table->colVarChar('goods_sku', 20)->setDefaultValue('')->setColumnComment('商品SKU');
        $table->colVarChar('goods_code', 20)->setDefaultValue('')->setColumnComment('商品编号');
        $table->colVarChar('goods_spec', 100)->setDefaultValue('')->setColumnComment('商品规格');
        $table->colVarChar('goods_name', 500)->setDefaultValue('')->setColumnComment('商品名称');
        $table->colVarChar('goods_cover', 999)->setDefaultValue('')->setColumnComment('商品封面');
        $table->colVarChar('goods_payment', 999)->setDefaultValue('')->setColumnComment('支付方式');
        $table->colDecimal('price_selling',20,2)->setDefaultValue(0.00)->setColumnComment('销售价格');
        $table->colDecimal('price_market',20,2)->setDefaultValue(0.00)->setColumnComment('市场价格');
        $table->colDecimal('total_selling',20,2)->setDefaultValue(0.00)->setColumnComment('销售总价');
        $table->colDecimal('total_market',20,2)->setDefaultValue(0.00)->setColumnComment('市场总价');
        $table->colDecimal('reward_balance',20,2)->setDefaultValue(0.00)->setColumnComment('奖励余额');
        $table->colDecimal('reward_integral',20,2)->setDefaultValue(0.00)->setColumnComment('奖励积分');
        $table->colBigInt('stock_sales',20)->setDefaultValue(1)->setColumnComment('商品数量');
        $table->colVarChar('vip_name',30)->setDefaultValue('')->setColumnComment('用户等级名称');
        $table->colBigInt('vip_code',20)->setDefaultValue(0)->setColumnComment('用户等级序号');
        $table->colTinyInt('vip_entry',1)->setDefaultValue(0)->setColumnComment('入会礼包(0非入会礼包,1是入会礼包)');
        $table->colBigInt('vip_upgrade', 20)->setDefaultValue(0)->setColumnComment('购买升级等级(0不升级,其他升级)');
        $table->colVarChar('truck_code', 20)->setDefaultValue('')->setColumnComment('物流运费模板');
        $table->colTinyInt('truck_type',1)->setDefaultValue(0)->setColumnComment('物流配送(0无需配送,1需要配送)');
        $table->colBigInt('truck_number',20)->setDefaultValue(0)->setColumnComment('快递计费基数');
        $table->colTinyInt('rebate_type',1)->setDefaultValue(1)->setColumnComment('参与返利(0无需返利,1需要返利)');
        $table->colDecimal('rebate_amount',20,2)->setDefaultValue(0.00)->setColumnComment('参与返利金额');
        $table->colBigInt('discount_id', 20)->setDefaultValue(0)->setColumnComment('折扣方案编号');
        $table->colDecimal('discount_rate',20,6)->setDefaultValue(0.00)->setColumnComment('销售价格折扣');
        $table->colDecimal('discount_amount',20,2)->setDefaultValue(0.00)->setColumnComment('商品优惠金额');
        $table->colTinyInt('status',1)->setDefaultValue(1)->setColumnComment('商品状态(1使用,0禁用)');
        $table->colTinyInt('is_deleted', 1)->setDefaultValue(0)->setIsNotNull()->setColumnComment('是否删除：0.未删除，1.已删除');
        $table->colInt('create_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('创建时间戳');
        $table->colInt('update_time', 10)->setDefaultValue(0)->setIsNotNull()->setColumnComment('更新时间戳');

        $table->indexNormal('idx_shop_order_item_status', 'status');
        $table->indexNormal('idx_shop_order_item_deleted', 'is_deleted');
        $table->indexNormal('idx_shop_order_item_order_no', 'order_no');
        $table->indexNormal('idx_shop_order_item_goods_sku', 'goods_sku');
        $table->indexNormal('idx_shop_order_item_goods_code', 'goods_code');
        $table->indexNormal('idx_shop_order_item_goods_spec', 'goods_spec');
        $table->indexNormal('idx_shop_order_item_rebate_type', 'rebate_type');
        return $table;
    }
}